import { EventTouch } from "cc"
import { Widget } from "cc"
import { game } from "cc"
import { UITransform } from "cc"
import { _decorator, Component, Node } from "cc"
import { gtPlayable } from "./GtPlayable"
const { ccclass, property } = _decorator

@ccclass("FirstTouchEvent")
export class FirstTouchEvent extends Component {
  private nodeTouch: Node = null
  start() {
    if(gtPlayable.channel == "AppLovin" || gtPlayable.channel == "Google"){
      this.nodeTouch = new Node()
      this.nodeTouch.setParent(this.node)
      this.nodeTouch.setPosition(0, 0, 0)
      const widget = this.nodeTouch.addComponent(Widget)
      widget.isAlignLeft = true
      widget.isAlignRight = true
      widget.isAlignBottom = true
      widget.isAlignTop = true
      widget.left = 0
      widget.right = 0
      widget.bottom = 0
      widget.top = 0
      this.nodeTouch.on(Node.EventType.TOUCH_START, (event: EventTouch) => {
        event.preventSwallow = true;
        game.emit("onFirstTouchStart")
        this.nodeTouch.destroy()
        this.nodeTouch = null
      },this)
    }else{
      this.destroy()
    }

  }


  protected update(dt: number): void {
    if (this.nodeTouch && this.nodeTouch.active) {
      this.nodeTouch.setSiblingIndex(this.node.children.length - 1)
    }
  }
}
